package com.bw.ivideo

import android.os.Handler
import android.os.Message
import android.os.Trace
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentPagerAdapter
import androidx.viewpager.widget.ViewPager
import com.baweigame.xmpplibrary.XmppManager
import com.bw.cinema.ui.CinemaFragment
import com.bw.common.utils.LogUtils
import com.bw.common.utils.UserUtils
import com.bw.home.ui.HomeFragment
import com.bw.ivideo.databinding.ActivityMainBinding
import com.bw.live.ui.LiveFragment
import com.bw.mine.ui.MineFragment
import com.bw.mvicore.ui.BaseActivity
import com.bw.publish.ui.PublishFragment
import java.lang.ref.ReferenceQueue
import java.lang.ref.SoftReference

class MainActivity : BaseActivity() {
    private val TAG:String=MainActivity::class.java.name
    lateinit var binding: ActivityMainBinding

    override fun releaseRes() {

    }

    override fun generateContentView(): View {
        binding = ActivityMainBinding.inflate(layoutInflater)
        return binding.root
    }

    private var isXMPPLogin:Boolean = false
    override fun initView() {
        super.initView()

        Trace.beginSection("BW2110")

        initPageFramework()
        val user = UserUtils.getCurrentUser()
        if (user!=null){
//            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
//                Looper.getMainLooper().queue.addIdleHandler {
//
//                    true
//                }
//            }
            if (!isXMPPLogin){
                generateLifeCycleLaunch {
                    //登录xmpp服务器
                    XmppManager.getInstance().xmppUserManager.login(user.username,user.password)
                    isXMPPLogin=true
                }
            }
        }
        mHandler.postDelayed(object :Runnable{
            override fun run() {
                LogUtils.d("123","handler ......")
                mHandler.postDelayed(this,1000)
            }

        },1000)

    }
//
    private val mHandler: Handler = object :Handler(){
    override fun handleMessage(msg: Message) {
        super.handleMessage(msg)

    }


}

    override fun onWindowFocusChanged(hasFocus: Boolean) {
        super.onWindowFocusChanged(hasFocus)
        Trace.endSection()
    }

    override fun onStop() {
        super.onStop()
//        mHandler.removeCallbacksAndMessages(null)
    }

    override fun initData() {

    }

    override fun initEvent() {
        binding.vpMain.addOnPageChangeListener(object :ViewPager.OnPageChangeListener{
            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {

            }

            override fun onPageSelected(position: Int) {
                binding.bnvBar.menu.getItem(position).isChecked=true
            }

            override fun onPageScrollStateChanged(state: Int) {

            }

        })

        binding.bnvBar.setOnNavigationItemSelectedListener {
             binding.vpMain.currentItem=it.order

             true
        }

        val queue:ReferenceQueue<AppCompatActivity> = ReferenceQueue()
        val ref = SoftReference<AppCompatActivity>(this,queue)
        val activity =  ref.get()


//        binding.bnvBar.setItemOnTouchListener(R.id.bar_home,object :View.OnTouchListener{
//            override fun onTouch(v: View?, event: MotionEvent?): Boolean {
//                if (event?.action==MotionEvent.ACTION_DOWN){
//                    //发送首页刷新事件
//                    EventBus.getDefault().post(HomeRefreshEvent())
//                }
//                return true
//            }
//        })


    }

    private fun initPageFramework() {

        val fragments = listOf<Fragment>(HomeFragment(),CinemaFragment(),PublishFragment(),LiveFragment(),MineFragment())
        binding.vpMain.adapter = PageAdapter(fragments,supportFragmentManager)
    }
    //
    class PageAdapter(val fragments:List<Fragment>, manager:FragmentManager):FragmentPagerAdapter(manager){
        override fun getCount(): Int {
            return fragments.size
        }

        override fun getItem(position: Int): Fragment {
            return fragments.get(position)
        }

    }

}